package allwe.reflact;

import org.springframework.util.StringUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ShenWei
 * @Date: 2024/07/18/14:46
 */
public class ListToTree {

    public static void main(String[] args) throws Exception {

        listToTree(MyTeamWorkAreaDTO.getList(), "areaCode", MyTeamWorkAreaDTO.class);
    }


    public static  <T> List<T> listToTree(List<T> list, String fieldName, Class<T> clazz) throws Exception {
        // 参数校验
        try {
            list.get(0).getClass().getDeclaredField(fieldName);
        } catch (NullPointerException e) {
            System.out.println("list must not be null");
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            System.out.println("未找到字段:" + fieldName);
            throw new RuntimeException(e);
        }
        // 准备返回值
        List<T> resList = new ArrayList<>();
        // 找到最上面一级
        for (T node : list) {
            Constructor<T> constructor = clazz.getConstructor();
            // 获取比较字段的值
            Field field = node.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            String areaCode = (String) field.get(constructor.newInstance());
            // 获取当前行政区划节点等级
            int regionLevel = getRegionLevel(areaCode);
            System.out.println(regionLevel);
        }
        return resList;
    }


    public static int getRegionLevel(String regionCode) {
        if (StringUtils.isEmpty(regionCode) || regionCode.startsWith("86") || regionCode.length() > 12) {
            return 0;
        }
        regionCode = padAfter(regionCode, 12, '0');
        if (regionCode.substring(2).equals("0000000000")) {
            return 1;
        } else if (regionCode.substring(4).equals("00000000")) {
            return 2;
        } else if (regionCode.substring(6).equals("000000")) {
            return 3;
        } else if (regionCode.substring(9).equals("000")) {
            return 4;
        } else if (regionCode.length() == 15) {
            return 6;
        }
        return 5;
    }


    public static String padAfter(CharSequence str, int length, char padChar) {
        if (null == str) {
            return null;
        } else {
            int strLen = str.length();
            if (strLen == length) {
                return str.toString();
            } else {
                return strLen > length ? sub(str, strLen - length, strLen) : str.toString().concat(repeat(padChar, length - strLen));
            }
        }
    }

    public static String sub(CharSequence str, int fromIndexInclude, int toIndexExclude) {
        if (isEmpty(str)) {
            return str(str);
        } else {
            int len = str.length();
            if (fromIndexInclude < 0) {
                fromIndexInclude += len;
                if (fromIndexInclude < 0) {
                    fromIndexInclude = 0;
                }
            } else if (fromIndexInclude > len) {
                fromIndexInclude = len;
            }

            if (toIndexExclude < 0) {
                toIndexExclude += len;
                if (toIndexExclude < 0) {
                    toIndexExclude = len;
                }
            } else if (toIndexExclude > len) {
                toIndexExclude = len;
            }

            if (toIndexExclude < fromIndexInclude) {
                int tmp = fromIndexInclude;
                fromIndexInclude = toIndexExclude;
                toIndexExclude = tmp;
            }

            return fromIndexInclude == toIndexExclude ? "" : str.toString().substring(fromIndexInclude, toIndexExclude);
        }
    }

    public static String repeat(char c, int count) {
        if (count <= 0) {
            return "";
        } else {
            char[] result = new char[count];

            for(int i = 0; i < count; ++i) {
                result[i] = c;
            }

            return new String(result);
        }
    }

    public static boolean isEmpty(CharSequence str) {
        return str == null || str.length() == 0;
    }

    public static String str(CharSequence cs) {
        return null == cs ? null : cs.toString();
    }
}
